package cn.com.scitc.licenseplateback.controller;

import cn.com.scitc.licenseplateback.dao.CityCodeDao;
import cn.com.scitc.licenseplateback.dao.LdleParkingDao;
import cn.com.scitc.licenseplateback.dao.NationalTreasuryDao;
import cn.com.scitc.licenseplateback.model.CityCodeEntity;
import cn.com.scitc.licenseplateback.model.LdleParkingEntity;
import cn.com.scitc.licenseplateback.model.NationalTreasuryEntity;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class AppPage {

    /**
     * 查询全国城市代码
     */
    @Autowired
    private CityCodeDao cityCodeDao;

    /**
     * 查询各城市立方库信息
     */
    @Autowired
    private NationalTreasuryDao nationalTreasuryDao;

    /**
     * 查询各立方库的剩余车位信息
     */
    @Autowired
    private LdleParkingDao ldleParkingDao;


    @GetMapping("/")
    private Object index(String address, int start) {
        // 根据获取城市名 获取 城市编号
        CityCodeEntity byCitycode = cityCodeDao.findByCitycode(address);
        // 根据城市编号 获取 城市内的立方库信息列表
//        List<NationalTreasuryEntity> CubeList = nationalTreasuryDao.findAllByCurrentPosCube(Integer.valueOf(byCitycode.getCitycode()));
        List<NationalTreasuryEntity> CubeList = nationalTreasuryDao.findByCurrentLimitCube(byCitycode.getCitycode().toString(), start, 4);

        // 根据立方库列表信息 获取 每个立方库车位情况
        List<List<LdleParkingEntity>> allByCubeInfo = new ArrayList<>();
        for (NationalTreasuryEntity cube : CubeList) {
            List<LdleParkingEntity> CubeInfo = new ArrayList<>();
            CubeInfo = ldleParkingDao.findAllByCubeInfo(cube.getCubeLibraryNumber());
            allByCubeInfo.add(CubeInfo);
        }

        // 存储 返回客户端信息
        List<List<String>> list = new ArrayList<>(4);
        //统计每个立方库空闲车位信息并将重要信息进行拼接
        int total = 0;
        for (NationalTreasuryEntity nte : CubeList) {
            List<String> sublist = new ArrayList<>();
            sublist.add(String.valueOf(nte.getAutoNumber()));
            sublist.add(nte.getCubeLibraryNumber());
            sublist.add(nte.getCubeLibraryAddress());
            for (List<LdleParkingEntity> lpe : allByCubeInfo) {
                for (LdleParkingEntity sublpe : lpe) {
                    if (nte.getCubeLibraryNumber().equals(sublpe.getCubeLibraryNumber())) {
                        String str = sublpe.getNumber().substring(1, sublpe.getNumber().length() - 2);
                        total += str.split(",").length;
                    }
                }
            }
            sublist.add(String.valueOf(total));
            list.add(sublist);
            total = 0;
        }
        // 将列表数据 进行 JSON 格式化转换
        Object JsonList = JSON.toJSON(list);

        return JsonList;
    }

}

